********************************************************************************
* MANAGE LISA RAW DATA  
* File description: Managing Lisa, KPI etc. creating the correct variables 
********************************************************************************

*****CPI******

local	cpi2017	0.982366272
local	cpi2016	1
local	cpi2015	1.00982926440083
local	cpi2014	1.00937828957861
local	cpi2013	1.00754632872699
local	cpi2012	1.00709739019733
local	cpi2011	1.0160549722249
local	cpi2010	1.04274039412114
local	cpi2009	1.05596342521524
local	cpi2008	1.05262632646951
local	cpi2007	1.08922240198272
local	cpi2006	1.11332770389135
local	cpi2005	1.12849500713267
local	cpi2004	1.1333452722063
local	cpi2003	1.13782811938152

set more off

forval i=2003(1)2017 { 

use "raw/LISA/lisa_`i'.dta", clear

gen year=`i' 


if year<=2003 {
	
gen dispinc=DispInk*`kpi`i''
label var dispinc "CPI 2016 adjusted disposible income"
drop DispInk

gen dispinch=DispInkFam*`kpi`i''
label var dispinch "CPI 2016 adjusted household disposible income"
drop DispInkFam	
	
}

if year>=2004 & year<=2015 {
gen dispinc=DispInk04*`kpi`i''
label var dispinc "CPI 2016 adjusted disposible income"
drop DispInk04

gen dispinch=DispInkFam04*`kpi`i''
label var dispinch "CPI 2016 adjusted household disposible income"
drop DispInkFam04


}

if year>=2016  {
destring Dispink04, replace
destring DispinkFam04, replace
gen dispinc=Dispink04*`kpi`i''
label var dispinc "CPI 2016 adjusted disposible income"
drop Dispink04

gen dispinch=DispinkFam04*`kpi`i''
label var dispinch "CPI 2016 adjusted household disposible income"
drop DispinkFam04

rename Lopnr_PersonNr LopNr
}

replace dispinc=dispinc*100
replace dispinch=dispinch*100

rename LopNr_CfarNr CfarNr

rename Alder age
destring age, replace

drop if age<15

destring AstNr, replace
destring CfarNr, replace

label var AstNr "ID for work place"

label var CfarNr "ID for work place"

label var LopNr "ID for individual"

rename Kommun municipality
destring municipality, replace
replace municipality=331 if municipality==1917 


keep LopNr year age dispinc dispinch municipality AstNr CfarNr


tempfile lisa`i'
save `lisa`i''

}

foreach i of numlist 2003/2016 {
append using `lisa`i'' 

}

duplicates drop LopNr year, force
save "use/temp/lisa_2003_2017", replace

forval i=2003(1)2017 { 

use lopnr DeSO using "raw/DeSo/deso_`i'.dta", clear

gen year=`i' 


rename lopnr LopNr

rename DeSO deso
destring deso, replace


tempfile deso`i'
save `deso`i''

}

foreach i of numlist 2003/2016 {
append using `deso`i'' 

}

duplicates drop LopNr year, force

merge 1:1 LopNr year using "use/temp/lisa_2003_2017"

keep if _merge==3
drop _merge

drop AstNr*

sort LopNr year

by LopNr: gen dispinc_lag=dispinc[_n-1]
by LopNr: gen dispinc_2lag=dispinc[_n-2]
by LopNr: gen dispinch_lag=dispinch[_n-1]
by LopNr: gen dispinch_2lag=dispinch[_n-2]

by LopNr: gen dispinc_permanent=(dispinc[_n-1]+dispinc[_n-2]+dispinc[_n-3])/3


save "use/final_for_yitz", replace






